﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien.Business;

namespace QuanLyThuVien.Presentation
{
    public partial class frmQLDauSach : Form
    {
        public frmQLDauSach()
        {
            InitializeComponent();
        }
        DauSachBUS SachBus = new DauSachBUS();

        DauSach sach = new DauSach();

        NXBBUS NXB = new NXBBUS();

        TacGiaBUS TG = new TacGiaBUS();

        DauSachBUS BusDS = new DauSachBUS();

        private void btnSuaSach_Click(object sender, EventArgs e)
        {
                if (MessageBox.Show("Bạn có thực sự muốn sửa thông tin sách hay không?", "QuanLyThuVien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    sach.MaDauSach = txtMaDauSach.Text;
                    sach.TenSach = txtTenSach.Text;
                    sach.MaTG = txtMaTG.Text;
                    sach.MaNXB = txtMaNXB.Text;
                    sach.NamXB = txtNamXB.Text;
                    sach.ChuyenNganh = txtChuyenNganh.Text;
                    sach.SoLuong = txtSoLuong.Text;
                    sach.GhiChu = txtGhiChu.Text;
                    SachBus.SuaSach(sach);
                    SachBus.LayDLLenDataGridView(dgSachNhap);
                }
        }

        private void btnXoaSach_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có thực sự muốn xóa thông tin sách hay không?", "QuanLyThuVien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    SachBus.XoaSach(txtMaDauSach.Text);
                    SachBus.LayDLLenDataGridView(dgSachNhap);
                    lbSoLuong.Text = SachBus.TongSach(lbSoLuong.Text);
                }
            }
            catch
            {
                MessageBox.Show("Sách này đang được mượn, không thể xóa được.", "QuanLyThuVien");
            }
        }

        private void frmQLSach_Load(object sender, EventArgs e)
        {
            SachBus.LayDLLenDataGridView(dgSachNhap);
            NXB.LoadLenCB(cbNXB);
            TG.LoadLenCBTacGia(cbTenTG);
            lbSoLuong.Text = SachBus.TongSach(lbSoLuong.Text);
        }

        private void btnTaoMoiSach_Click(object sender, EventArgs e)
        {
            txtMaDauSach.Text = "";
            txtTenSach.Text = "";
            txtMaTG.Text = "";
            cbNXB.Text = "";
            txtMaNXB.Text = "";
            txtNamXB.Text = "";
            txtChuyenNganh.Text = "";
            txtSoLuong.Text = "";
            txtGhiChu.Text = "";
            cbTenTG.Text = "";
        }

        private void btnNhapSach_Click_1(object sender, EventArgs e)
        {
            try
            {
                sach.MaDauSach = txtMaDauSach.Text;
                sach.TenSach = txtTenSach.Text;
                sach.MaTG = txtMaTG.Text;
                sach.MaNXB = txtMaNXB.Text;
                sach.NamXB = txtNamXB.Text;
                sach.ChuyenNganh = txtChuyenNganh.Text;
                sach.SoLuong = txtSoLuong.Text;
                sach.GhiChu = txtGhiChu.Text;
                SachBus.ThemMoiSach(sach);
                SachBus.LayDLLenDataGridView(dgSachNhap);
                lbSoLuong.Text = SachBus.TongSach(lbSoLuong.Text);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "QuanLyThuVien");
            }
        }
        
        private void cbTenTG_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMaTG.Text = TG.LoadLenMaTG(txtMaTG.Text, cbTenTG.Text);
        }

        private void cbNXB_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMaNXB.Text = NXB.LoadLenMaNXB(txtMaNXB.Text, cbNXB.Text);
        }

        private void dgSachNhap_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int a = e.RowIndex;
            txtMaDauSach.Text = dgSachNhap[0, a].Value.ToString();
            txtTenSach.Text = dgSachNhap[1, a].Value.ToString();
            txtMaTG.Text = dgSachNhap[2, a].Value.ToString();
            txtMaNXB.Text = dgSachNhap[3, a].Value.ToString();
            txtNamXB.Text = dgSachNhap[4, a].Value.ToString();
            txtChuyenNganh.Text = dgSachNhap[5, a].Value.ToString();
            txtSoLuong.Text = dgSachNhap[6, a].Value.ToString();
            txtGhiChu.Text = dgSachNhap[7, a].Value.ToString();
        }

    }
}
